﻿using System;
using System.IO;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKArchUtils.Utils;
using YJKGeometryFuncs;
using YJKRevitDimensionText.OneKeyPlanDim.LevelDim;
using YJKPresentation.CommonUnit;

namespace YJKRevitDimensionText.OneKeyPlanDim.Utility
{
	public class SlopeDimData
	{
		public string DimVal
		{
			get
			{
				return this.m_strDimVal;
			}
			set
			{
				this.m_strDimVal = value;
			}
		}

		public XYZ PtStart
		{
			set
			{
				this.m_ptStart = value;
			}
		}

		public XYZ SlopeDimDir
		{
			set
			{
				this.m_SlopeDimDir = value;
			}
		}

		public SlopeDimData(Autodesk.Revit.DB.Document doc)
		{
			this.m_doc = doc;
		}

		public FamilySymbol GetSlopeDimFamilySymbol()
		{
			Transaction transaction = new Transaction(this.m_doc, "CreateDim");
			try
			{
				transaction.Start();
				string strFamilyFilePath = Path.Combine(Product.FamilyLocation, RevitVersion.Version, "OneKeyDim", "标高、坡道族、尺寸标注.rvt");
				SlopeDimData.m_familySymbol = FindFamilySymbol.GetFamilySymbol(this.m_doc, strFamilyFilePath, SlopeDimData.m_strFamilyName, SlopeDimData.m_strFamilySymbolName);
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
			return SlopeDimData.m_familySymbol;
		}

		public FamilyInstance CreateSlopeDim()
		{
			FamilyInstance familyInstance = null;
			Transaction transaction = new Transaction(this.m_doc, "SlopeDim");
			try
			{
				transaction.Start();
				if (SlopeDimData.m_familySymbol == null)
				{
					transaction.RollBack();
					return null;
				}
				if (SlopeDimData.m_familySymbol == null)
				{
					return null;
				}
				XYZ xyz = this.m_ptStart + this.m_SlopeDimDir;
				XYZ xyz2 = new XYZ(this.m_ptStart.X, this.m_ptStart.Y, 0.0);
				XYZ xyz3 = new XYZ(xyz.X, xyz.Y, 0.0);
				XYZ xyz4 = (xyz2 - xyz3).Normalize();
				double num = XYZ.BasisX.AngleTo(xyz4);
				XYZ xyz5 = XYZ.BasisZ;
                if (!YJKArchUtils.Utils.Geometry.IsParallel(xyz4, XYZ.BasisX, 1E-09))
				{
					xyz5 = XYZ.BasisX.CrossProduct(xyz4).Normalize();
				}
				Line line = Line.CreateUnbound(xyz2, xyz5);
				FamilyInstance familyInstance2 = this.m_doc.Create.NewFamilyInstance(xyz2, SlopeDimData.m_familySymbol, this.m_doc.ActiveView);
				Parameter parameter = RevitElementExtension.GetParameter(familyInstance2, "坡度上");
				if (parameter != null && this.m_strDimVal != "")
				{
					parameter.Set(this.m_strDimVal);
				}
				this.m_doc.Regenerate();
				familyInstance = familyInstance2;
				ElementTransformUtils.RotateElement(this.m_doc, familyInstance2.Id, line, num);
                if (YJKArchUtils.Utils.Geometry.LessThan(this.m_ptStart.X, xyz.X))
				{
					Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(this.m_ptStart, -this.m_SlopeDimDir, (this.m_ptStart.Add(this.m_doc.ActiveView.ViewDirection * 100.0) - this.m_ptStart).Normalize());
					familyInstance = MirrorOperater.MirrorParasiticElement(this.m_doc, familyInstance2, plane, false, true);
				}
				this.m_doc.Regenerate();
				double num2 = (double)familyInstance.Document.ActiveView.Scale;
				parameter = RevitElementExtension.GetParameter(familyInstance, "箭头长度");
				if (parameter == null)
				{
					return null;
				}
				double num3 = parameter.AsDouble() * num2;
				parameter = RevitElementExtension.GetParameter(familyInstance, "基线长度");
				if (parameter == null)
				{
					return null;
				}
				double num4 = parameter.AsDouble() * num2;
				ElementTransformUtils.MoveElement(this.m_doc, familyInstance.Id, -xyz4 * (num4 + num3));
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
			if (familyInstance != null)
			{
				KeyComponent.GetInstance().m_lstSlopeDimension.Add(familyInstance);
			}
			return familyInstance;
		}

		public static FamilySymbol m_familySymbol = null;

		public static string m_strFamilyFilePath = "C:\\Users\\zk\\Desktop\\带斜角的单侧实心箭头-仿宋-3.5-0.7.rfa";

		public static string m_strFamilyName = "HY-符号-坡度标注";

		public static string m_strFamilySymbolName = "实心箭头";

		private string m_strDimVal = string.Empty;

		private XYZ m_ptStart = XYZ.BasisX;

		private XYZ m_ptOrign = XYZ.Zero;

		private XYZ m_SlopeDimDir = XYZ.BasisX;

		private Document m_doc;
	}
}
